<template>
  <div class="manage">
    <el-dialog :visible.sync="dialogVisible" :title="operateType === 'add' ? '新增用户' : '修改用户'">
      <CommonForm :form="operateForm" :form-label="operateLabel"></CommonForm>
    </el-dialog>
    <div class="manage-header">
      <el-button type="primary" @click="add">+ 新增</el-button>
      <CommonForm inline :form="form" :form-label="formLabel">
        <el-button type="primary">搜索</el-button>
      </CommonForm>
    </div>
    <CommonTable :tableData="tableData" :tableLabel="tableLabel" :config="config" @changePage="changePage" @edit="editUser" @del="delUser"></CommonTable>
  </div>
</template>

<script>
import CommonForm from '@/components/CommonForm.vue'
import CommonTable from '@/components/CommonTable.vue'

export default {
  components: { CommonForm, CommonTable },
  data() {
    return {
      form: {
        keyword: '',
      },
      formLabel: [
        {
          label: '',
          model: 'keyword',
        },
      ],
      tableData: [],
      tableLabel: [
        { prop: 'name', label: '姓名' },
        { prop: 'sexLabel', label: '姓别' },
        { prop: 'age', label: '年龄' },
        { prop: 'birth', label: '生日' },
        { prop: 'addr', label: '地址' },
      ],
      config: {
        loading: false,
        page: 1,
        total: 30,
      },
      operateType: 'add',
      operateForm: {
        name: '',
        addr: '',
        age: '',
        birth: '',
        // sex: '',
      },
      operateLabel: [
        {
          model: 'name',
          label: '姓名',
        },
        {
          model: 'age',
          label: '年龄',
        },
        // {
        //   model: 'sex',
        //   label: '性别',
        //   type: 'select',
        //   opts: [
        //     {
        //       label: '男',
        //       value: 1,
        //     },
        //     {
        //       label: '女',
        //       value: 0,
        //     },
        //   ],
        // },
        {
          model: 'birth',
          label: '出生日期',
          type: 'date',
        },
        {
          model: 'addr',
          label: '地址',
        },
      ],
      dialogVisible: false,
    }
  },
  mounted() {
    this.getUserList()
  },
  methods: {
    getUserList() {
      this.config.loading = true
      this.$http
        .get('/user/getUser', {
          params: {
            page: this.config.page,
          },
        })
        .then((res) => {
          this.tableData = res.data.list.map((item) => {
            item.sexLabel = item.sex === 0 ? '女' : '男'
            return item
          })
          this.config.total = res.data.count
          this.config.loading = false
        })
    },
    changePage(val) {
      this.config.page = val
      // console.log(val, 'val')
      this.getUserList()
    },
    add() {
      // this.operateType = 'add'
      this.dialogVisible = true
    },
    editUser(row) {
      this.operateType = 'edit'
      this.dialogVisible = true
      // console.log(row)
      this.operateForm = row
    },
    delUser(row) {
      console.log(row)
    },
  },
}
</script>

<style lang="scss" scoped>
@import '~@/assets/scss/Common';
</style>
